package com.cqupt.controller;

import com.cqupt.common.*;
import com.cqupt.pojo.TbUser;
import com.cqupt.service.ProductService;
import com.cqupt.service.UserService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    private UserService userService;

    @Autowired
    private ProductService productService;

    @RequestMapping("/index")
    public String index() {
        return "index";
    }

    @RequestMapping("/userList")
    public String userList() {
        return "userList";
    }

    @RequestMapping("/userListData")
    @ResponseBody
    public JsonData userListData(Pagination pagination) {
        JsonData jsonData = new JsonData();

        Page page = userService.queryPageList(pagination);
        jsonData.setData(page.getResult());
        jsonData.setCount(page.getTotal());
        return jsonData;
    }

    @RequestMapping(value = "/userEdit",method = RequestMethod.GET)
    public String userEdit() {
        return "userEdit";
    }

    @RequestMapping(value = "/userEdit",method = RequestMethod.POST)
    @ResponseBody
    public Result userEdit_submit(TbUser tbuser) {
        Result result = new Result();

        System.out.println("userid:" + tbuser.getId());

        if(tbuser.getId()==null) {  // 添加用户
            tbuser.setUserPwd("123456");
            userService.insertUser(tbuser);
        } else {    // 编辑用户
            userService.updateUser(tbuser);
        }

        result.setCode(0);
        result.setMsg("");
        return result;
    }

    @RequestMapping(value = "/userDelete",method = RequestMethod.POST)
    @ResponseBody
    public Result userDelete(Integer userid) {
        Result result = new Result();

        if(userid==1) {
            result.setCode(1);
            result.setMsg("不允许删除admin");
        } else {
            userService.deleteUser(userid);

            result.setCode(0);
            result.setMsg("");
        }

        return result;
    }

    @RequestMapping("/userData")
    @ResponseBody
    public TbUser userData(Integer userid) {
        return userService.queryUser(userid);
    }

    //对BOSS系统管理功能模块下的事件进行管理　　start

       //产品管理界面跳转
    @RequestMapping(value = "/productManagement",method = RequestMethod.GET)
    public String productManagement() {
        return "productManagement";
    }

    //返回产品详细信息界面
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public String  detail(Integer productid, HttpServletRequest request){
        Map productData = productService.prouductData(productid);
        request.setAttribute("productData",productData);
        return "detail";
    }
    //编辑返回产品详细信息界面
    @RequestMapping(value = "/detailEdit",method = RequestMethod.GET)
    @ResponseBody
    public Product detailEdit(Integer productid){
        Product product = productService.productListData(productid);
        return product;
    }


    //产品信息编辑界面
    @RequestMapping(value = "/productEdit",method = RequestMethod.GET)
    public String productEdit(){
        return "productEdit";
    }

    //添加产品
    @RequestMapping(value = "/productEdit",method = RequestMethod.POST)
    @ResponseBody
    public Result productEdit_submitt(Product product){
        Result result = new Result();
        Integer productId = product.getProductId();
        product.setDepositoryName("测试仓库1");
        if(productId == null){//添加产品
            int i = productService.productAdd(product);
            if(i>0){
                result.setCode(0);
                result.setMsg("添加成功");
            }
        } else {//编辑产品

        }
        return result;
    }

    @RequestMapping("/productQueryList")
    @ResponseBody
    public JsonData productQueryList(Pagination pagination){
        JsonData jsonData = new JsonData();
        List data = productService.productQueryList(pagination);
        jsonData.setMsg("成功查询");
        jsonData.setCount(1);
        jsonData.setData(data);
        return jsonData;
    }


       //资费管理界面跳转
    @RequestMapping(value = "/costManagement",method = RequestMethod.GET)
    public String costManagement() {
        return "costManagement";
    }
       //系统维护界面跳转
    @RequestMapping(value = "/systemMaintenance",method = RequestMethod.GET)
    public String systemMaintenance() {
        return "systemMaintenance";
    }
       //CA授权界面跳转
    @RequestMapping(value = "/CAAuthorization",method = RequestMethod.GET)
    public String CAAuthorization() {
        return "CAAuthorization";
    }

    //对BOSS系统管理功能模块下的事件进行管理　　end
}
